/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pehulja.mmdo.coursework.gui;

import com.pehulja.mmdo.coursework.math.Equation;
import com.pehulja.mmdo.coursework.math.GoalFunction;
import com.pehulja.mmdo.coursework.math.LinearProgramTask;
import com.pehulja.mmdo.coursework.math.PolinomialFunc;
import com.pehulja.mmdo.coursework.math.SimplexTable;
import com.pehulja.mmdo.coursework.math.Solver;
import com.pehulja.mmdo.coursework.math.Term;
import com.pehulja.mmdo.coursework.math.checkingResults.AdmissibilityChecking;
import com.pehulja.mmdo.coursework.math.checkingResults.MusculoskeletalChecking;
import com.pehulja.mmdo.coursework.math.exceptions.AlgoritmExceptions.InitialPlanCannotbeFinded;
import com.pehulja.mmdo.coursework.math.exceptions.AlgoritmExceptions.LimitlessnessOfLinearForm;
import com.pehulja.mmdo.coursework.math.exceptions.InvalidNumberArgmentsException;
import com.pehulja.mmdo.coursework.math.exceptions.TaskHasNotSpecialForm;
import com.pehulja.mmdo.coursework.math.matrix.Matrix;
import com.pehulja.mmdo.coursework.math.matrix.NoSquareException;
import com.pehulja.mmdo.coursework.reports.PdfReportCalculation;
import java.awt.Component;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author pehulja
 */
public class IndexFrame extends javax.swing.JFrame {

    /**
     * Creates new form IndexFrame
     */
    private SimplexTable simplexTable;
    private Solver solver;
    //Checkings
    private boolean musculoskeletalCheckingResult;
    private boolean admissibilityCheckingResult;
    private MusculoskeletalChecking musculoskeletalChecking;
    private boolean isOptimalPlanFinded;

    public IndexFrame() {
        directFunctionTableModel = new DirectFunctionTableModel();
        businessRestrictionsTableModel = new BusinessRestrictionsTableModel();
        initComponents();
        customInitComponents();
        this.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jScrollPane2 = new javax.swing.JScrollPane();
        BusinessRestrictionsTable = new javax.swing.JTable();
        BusinessRestrictionsTable1 = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jScrollPane1 = new javax.swing.JScrollPane();
        directFunctionTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        bAddColumn = new javax.swing.JButton();
        bRemoveColumn = new javax.swing.JButton();
        bAddBusinessRestriction = new javax.swing.JButton();
        bRemoveBusinessRestriction = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        lOptimalBasis = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        lOptimalPlanDirectTask = new javax.swing.JLabel();
        lOptimalValueDirectFunction = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        lAccebilityResult = new javax.swing.JLabel();
        bAdmissibilityCheck = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        lResultCheckForMusculoskeletal = new javax.swing.JLabel();
        bMusculoskeletalCheck = new javax.swing.JButton();
        jPanel7 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        lResultCheckForOptimality = new javax.swing.JLabel();
        bOptimalityCheck = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lSimplexTableNumber = new javax.swing.JLabel();
        lCalculatingTime = new javax.swing.JLabel();
        bSolveTask = new javax.swing.JButton();
        jPanel8 = new javax.swing.JPanel();
        bCheckingFoothold = new javax.swing.JButton();
        bGenerateReport = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        miCreateNewTask = new javax.swing.JMenuItem();
        miLoadTask = new javax.swing.JMenuItem();
        miSaveTask = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        miSolveTask = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Решение задачи линейного программирования");
        setLocationByPlatform(true);
        setState(JFrame.MAXIMIZED_BOTH);

        BusinessRestrictionsTable.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        BusinessRestrictionsTable.setModel(businessRestrictionsTableModel);
        BusinessRestrictionsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        BusinessRestrictionsTable.setRowHeight(20);
        jScrollPane2.setViewportView(BusinessRestrictionsTable);

        BusinessRestrictionsTable1.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        BusinessRestrictionsTable1.setModel(businessRestrictionsTableModel);
        BusinessRestrictionsTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        BusinessRestrictionsTable1.setRowHeight(20);
        jScrollPane2.setViewportView(BusinessRestrictionsTable1);

        jToolBar1.setRollover(true);

        directFunctionTable.setFont(new java.awt.Font("Tahoma", 0, 15)); // NOI18N
        directFunctionTable.setModel(directFunctionTableModel);
        directFunctionTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        directFunctionTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        directFunctionTable.setDragEnabled(true);
        directFunctionTable.setRowHeight(20);
        jScrollPane1.setViewportView(directFunctionTable);

        jLabel1.setText("Целевая функция");

        jLabel2.setText("Ограничения");

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("2. Редактирование задачи"));

        bAddColumn.setText("+1 Переменная");
        bAddColumn.setFocusable(false);
        bAddColumn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        bAddColumn.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        bAddColumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addColumnActionPerformed(evt);
            }
        });

        bRemoveColumn.setText("-1 Переменная");
        bRemoveColumn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoveColumnActionPerformed(evt);
            }
        });

        bAddBusinessRestriction.setText("+1 Ограничение");
        bAddBusinessRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddBusinessRestrictionActionPerformed(evt);
            }
        });

        bRemoveBusinessRestriction.setText("-1 Ограничение");
        bRemoveBusinessRestriction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRemoveBusinessRestrictionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bAddBusinessRestriction, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bRemoveBusinessRestriction, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bRemoveColumn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bAddColumn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(bAddColumn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bRemoveColumn)
                .addGap(23, 23, 23)
                .addComponent(bAddBusinessRestriction)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bRemoveBusinessRestriction))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("4. Результат вычислений:"));

        jLabel4.setText("Оптимальное значение целевой функции");

        jLabel5.setText("Оптимальный базис");

        jLabel6.setText("Оптимальный план прямой задачи");

        lOptimalBasis.setText("Произведите вычисления");
        jScrollPane4.setViewportView(lOptimalBasis);

        lOptimalPlanDirectTask.setText("Произведите вычисления");
        jScrollPane5.setViewportView(lOptimalPlanDirectTask);

        lOptimalValueDirectFunction.setText("Произведите вычисления");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 250, Short.MAX_VALUE)
                    .addComponent(jScrollPane5)
                    .addComponent(lOptimalValueDirectFunction, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lOptimalValueDirectFunction))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("6. Результаты проверок"));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Проверка допустимости решения"));

        jLabel8.setText("Результат:");

        lAccebilityResult.setText("Произведите вычисления");

        bAdmissibilityCheck.setText("Подробнее...");
        bAdmissibilityCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAdmissibilityCheckActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(lAccebilityResult, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(bAdmissibilityCheck)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(lAccebilityResult))
                .addGap(18, 18, 18)
                .addComponent(bAdmissibilityCheck))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Проверка опорности"));

        jLabel11.setText("Результат:");

        lResultCheckForMusculoskeletal.setText("Произведите вычисления");

        bMusculoskeletalCheck.setText("Подробнее...");
        bMusculoskeletalCheck.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bMusculoskeletalCheckActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jLabel11)
                .addGap(18, 18, 18)
                .addComponent(lResultCheckForMusculoskeletal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(bMusculoskeletalCheck)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(lResultCheckForMusculoskeletal))
                .addGap(18, 18, 18)
                .addComponent(bMusculoskeletalCheck))
        );

        jPanel7.setBorder(javax.swing.BorderFactory.createTitledBorder("Проверка оптимальности"));

        jLabel12.setText("Результат:");

        lResultCheckForOptimality.setText("Произведите вычисления");

        bOptimalityCheck.setText("Подробнее...");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addComponent(jLabel12)
                .addGap(18, 18, 18)
                .addComponent(lResultCheckForOptimality, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(68, 68, 68)
                .addComponent(bOptimalityCheck)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(lResultCheckForOptimality))
                .addGap(18, 18, 18)
                .addComponent(bOptimalityCheck))
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("5. Показатели вычислений"));

        jLabel9.setText("Количество симплекс таблиц");

        jLabel3.setText("Время затраченное на вычисления (мс)");

        lSimplexTableNumber.setText("Произведите вычисления");

        lCalculatingTime.setText("Произведите вычисления");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lSimplexTableNumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lCalculatingTime, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(lSimplexTableNumber))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lCalculatingTime))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        bSolveTask.setBackground(new java.awt.Color(204, 0, 0));
        bSolveTask.setText("3. Решить задачу");
        bSolveTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSolveTaskActionPerformed(evt);
            }
        });

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Опорность"));

        bCheckingFoothold.setText("Проверка опорности");
        bCheckingFoothold.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bCheckingFootholdActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(bCheckingFoothold)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel8Layout.createSequentialGroup()
                .addGap(0, 29, Short.MAX_VALUE)
                .addComponent(bCheckingFoothold))
        );

        bGenerateReport.setText("7. Получить полный отчет");
        bGenerateReport.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bGenerateReportActionPerformed(evt);
            }
        });

        jMenu1.setText("1. Задача ...");

        miCreateNewTask.setText("Создать новую задачу");
        miCreateNewTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miCreateNewTaskActionPerformed(evt);
            }
        });
        jMenu1.add(miCreateNewTask);

        miLoadTask.setText("Загрузить ...");
        miLoadTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miLoadTaskActionPerformed(evt);
            }
        });
        jMenu1.add(miLoadTask);

        miSaveTask.setText("Сохранить ...");
        miSaveTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miSaveTaskActionPerformed(evt);
            }
        });
        jMenu1.add(miSaveTask);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Решение ...");

        miSolveTask.setText("2. Решить задачу");
        miSolveTask.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miSolveTaskActionPerformed(evt);
            }
        });
        jMenu2.add(miSolveTask);

        jMenuItem1.setText("7. Получить полный отчет в формате pdf");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Проверки...");

        jMenuItem2.setText("Допустимости решения");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem2);

        jMenuItem3.setText("На опорность решения");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem3);

        jMenuItem4.setText("На оптимальность");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem4);

        jMenuBar1.add(jMenu3);

        jMenu4.setText("Область устойчивости");
        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
                                    .addComponent(jScrollPane2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(bSolveTask, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(bGenerateReport, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))))
                        .addGap(101, 101, 101))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(1682, 1682, 1682)
                        .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(516, 516, 516))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(6, 6, 6)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bSolveTask)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(bGenerateReport))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addColumnActionPerformed
        directFunctionTableModel.addColumnToEnd();
        businessRestrictionsTableModel.addColumnToEnd();
    }//GEN-LAST:event_addColumnActionPerformed

    private void bRemoveColumnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoveColumnActionPerformed
        try {
            directFunctionTableModel.removePreLastColumn();
            businessRestrictionsTableModel.removePreLastColumn();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Ошибка", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_bRemoveColumnActionPerformed

    private void miSaveTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSaveTaskActionPerformed
        saveTask();
    }//GEN-LAST:event_miSaveTaskActionPerformed

    private void miLoadTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miLoadTaskActionPerformed
        loadTask();
    }//GEN-LAST:event_miLoadTaskActionPerformed

    private void miCreateNewTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miCreateNewTaskActionPerformed
        createNewtask();
    }//GEN-LAST:event_miCreateNewTaskActionPerformed

    private void bAddBusinessRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddBusinessRestrictionActionPerformed
        businessRestrictionsTableModel.appendEmptyDataRow();
    }//GEN-LAST:event_bAddBusinessRestrictionActionPerformed

    private void bRemoveBusinessRestrictionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRemoveBusinessRestrictionActionPerformed
        businessRestrictionsTableModel.removeLastDataRow();
    }//GEN-LAST:event_bRemoveBusinessRestrictionActionPerformed

    private void bSolveTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSolveTaskActionPerformed
        solveTask();
    }//GEN-LAST:event_bSolveTaskActionPerformed

    private void bMusculoskeletalCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bMusculoskeletalCheckActionPerformed
        musculoskeletalChecking();
    }//GEN-LAST:event_bMusculoskeletalCheckActionPerformed

    private void bAdmissibilityCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAdmissibilityCheckActionPerformed
        admissavbilityChecking();
    }//GEN-LAST:event_bAdmissibilityCheckActionPerformed

    private void bCheckingFootholdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCheckingFootholdActionPerformed
        checkingFoothold();
    }//GEN-LAST:event_bCheckingFootholdActionPerformed

    private void bGenerateReportActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bGenerateReportActionPerformed
        generateReport();
    }//GEN-LAST:event_bGenerateReportActionPerformed

    private void miSolveTaskActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miSolveTaskActionPerformed
        solveTask();
    }//GEN-LAST:event_miSolveTaskActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        generateReport();        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem4ActionPerformed
    
    private void generateReport(){
       try {
            PdfReportCalculation report = new PdfReportCalculation();
            report.createReport(solver, "c:/FirstPdf.pdf");
        } catch (Exception ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    /**
     * @param args the command line arguments
     */
    private void saveTask(){
        JFileChooser jFileChooser1 = new JFileChooser();
        if (jFileChooser1.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            ObjectOutputStream oos = null;
            try {
                oos = new ObjectOutputStream(new FileOutputStream(jFileChooser1.getSelectedFile().getAbsolutePath() + ".dat"));
                oos.writeObject(marshallToTaskFromTables());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    oos.close();
                } catch (IOException ex) {
                    Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    private void loadTask(){
        javax.swing.JFileChooser jFileChooser1 = new JFileChooser();
        if (jFileChooser1.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(jFileChooser1.getSelectedFile().getAbsolutePath()));
                unmarshallToTaskFromTables((LinearProgramTask) ois.readObject());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "Файл не найден", "Ошибка", JOptionPane.ERROR_MESSAGE);
            } catch (IOException ex) {
                Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            } catch(Exception ex){
                JOptionPane.showMessageDialog(null, "Неверный формат файла задачи", "Ошибка", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void customInitComponents() {
        directFunctionTableExtremum = new JComboBox(GoalFunction.Extremum.values());
        businessRestrictionsTableInequantity = new JComboBox(new Object[]{Equation.InequalitySight.BIGGER, Equation.InequalitySight.BIGGERorEQ});
        TableColumn column = directFunctionTable.getColumnModel().getColumn(directFunctionTable.getColumnCount() - 1);
        column.setCellEditor(new DefaultCellEditor(directFunctionTableExtremum));
        column = BusinessRestrictionsTable1.getColumnModel().getColumn(BusinessRestrictionsTable1.getColumnCount() - 2);
        column.setCellEditor(new DefaultCellEditor(businessRestrictionsTableInequantity));
   }
    private void checkingFoothold(){
         /*if (isOptimalPlanFinded) {
            solver.stabilityResearch(new Integer[]{1, 2});
            StabilityAreaDialog checkingDialog = new StabilityAreaDialog(this, true);
            checkingDialog.setSolver(solver);
            checkingDialog.setVisible(true);
        }*/
        StabilityAreaDialog checkingDialog = new StabilityAreaDialog(this, true);
        checkingDialog.addPoint(-2, 3);
        checkingDialog.addPoint(3, 1);
        checkingDialog.addPoint(0, -2);
        checkingDialog.addPoint(-3, -1);
        
        
             checkingDialog.getGraph().revalidate();
        checkingDialog.getGraph().repaint();
        checkingDialog.setVisible(true);
    }
    private void musculoskeletalChecking(){
        if (isOptimalPlanFinded) {
            
            MusculoskeletalCheckingDialog checkingDialog = new MusculoskeletalCheckingDialog(this, true);
            checkingDialog.setData(musculoskeletalChecking.getMatrixAfs(), solver.getBasisVectorsId());
            checkingDialog.setChecking(musculoskeletalChecking);
            checkingDialog.setVisible(true);
        }
    }
    private void admissavbilityChecking(){
        if (isOptimalPlanFinded) {
            AdmissibilityCheckingDialog checkingDialog = new AdmissibilityCheckingDialog(this, true);
            checkingDialog.setData(solver.getValuesOptimalResultPlanBasicComponents());
            checkingDialog.setResultChecing(admissibilityCheckingResult);
            checkingDialog.setVisible(true);
        }
    }
    private void createNewtask() {
        NewTaskDialog newTaskDialog = new NewTaskDialog(this, true);
        newTaskDialog.setVisible(true);
        if (newTaskDialog.isComitted()) {
            directFunctionTableModel = new DirectFunctionTableModel(newTaskDialog.getVarCount());
            businessRestrictionsTableModel = new BusinessRestrictionsTableModel(newTaskDialog.getVarCount(), newTaskDialog.getRestCount());

            directFunctionTable.setModel(directFunctionTableModel);
            BusinessRestrictionsTable1.setModel(businessRestrictionsTableModel);
            
            customInitComponents();
        }
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IndexFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IndexFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IndexFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IndexFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new IndexFrame().setVisible(true);
            }
        });
    }

    //logic
    private LinearProgramTask marshallToTaskFromTables() {
        LinearProgramTask task = null;
        GoalFunction gf = new GoalFunction();
        List<Term> gfComponents = new ArrayList<Term>();
        List<Equation> equations = new ArrayList<Equation>();
        int nVars = directFunctionTableModel.getColumnCount() - 1;
        int nRows = businessRestrictionsTableModel.getRowCount();
        try {
            for (int i = 0; i < nVars; i++) {
                gfComponents.add(new Term(getValueFromTable(directFunctionTableModel, 0, i), 1d, (byte) i));
            }
            gf.setComponents(gfComponents);
            gf.setExtremum((GoalFunction.Extremum) directFunctionTableModel.getValueAt(0, nVars));

            for (int i = 0; i < nRows; i++) {
                equations.add(new Equation());
                for (int j = 0; j < nVars; j++) {
                    equations.get(i).getLeft().getComponents().add(new Term(getValueFromTable(businessRestrictionsTableModel, i, j), 1d, (byte) j));
                }
                equations.get(i).getRight().getComponents().add(new Term(getValueFromTable(businessRestrictionsTableModel, i, nVars + 1), 0d, (byte) 0));
                equations.get(i).setInequalitySight((Equation.InequalitySight) businessRestrictionsTableModel.getValueAt(i, nVars));
            }

            task = new LinearProgramTask();
            task.setGoalFunction(gf);
            task.setBusinessRestrictions(equations);
            System.out.println(task);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Ошибка чтения данных с таблицы. Проверьте формат", "Ошибка", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        }
        return task;
    }

    private void unmarshallToTaskFromTables(LinearProgramTask task) {

        Vector<Object> gfData = new Vector<Object>();
        GoalFunction gf = task.getGoalFunction();
        for (Term i : gf.getComponents()) {
            gfData.add(i.getFactor());
        }
        gfData.add(gf.getExtremum());
        directFunctionTableModel.setDataVector(gfData);
        List<Equation> equations = task.getBusinessRestrictions();
        Vector<Vector<Object>> brData;
        brData = new Vector<Vector<Object>>();
        for (Equation equation : equations) {
            Vector<Object> temp = new Vector<Object>();
            for (Term i : equation.getLeft().getComponents()) {
                temp.add(i.getFactor());
            }
            temp.add(equation.getInequalitySight());
            temp.add(equation.getRight().getComponents().get(0).getFactor());
            brData.add(temp);
        }

        businessRestrictionsTableModel.setData(brData);
        directFunctionTableModel.fireTableStructureChanged();
        directFunctionTableModel.fireTableDataChanged();

        businessRestrictionsTableModel.fireTableStructureChanged();
        businessRestrictionsTableModel.fireTableDataChanged();
        customInitComponents();
    }

    private double getValueFromTable(AbstractTableModel model, int row, int col) throws Exception {
        Object value = model.getValueAt(row, col);
        if (value.getClass().equals(String.class)) {
            value = Double.parseDouble((String) value);
        }
        return ((Double) value).doubleValue();
    }
    private void solveTask(){
        String errorMessage = "";
        long startTime = 0, endTime = 0;

        AdmissibilityChecking admissibilityChecking;
        isOptimalPlanFinded = false;
        try {

            startTime = System.currentTimeMillis();
            solver = new Solver();
            solver.setTask(marshallToTaskFromTables());
            solver.solve();

            musculoskeletalChecking = MusculoskeletalChecking.getChecking();
            musculoskeletalChecking.setSolver(solver);
            musculoskeletalChecking.setField(lResultCheckForMusculoskeletal);
            musculoskeletalChecking.checkingForTheFoothold();

            admissibilityChecking = new AdmissibilityChecking(solver);
            admissibilityCheckingResult = admissibilityChecking.checkingForTheAdmissibility();
            endTime = System.currentTimeMillis();

            isOptimalPlanFinded = true;
        } catch (InvalidNumberArgmentsException ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Неверное число аргументов";
        } catch (CloneNotSupportedException ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Ошибка при клонировании объектов";
        } catch (LimitlessnessOfLinearForm ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Неограниченность линейной формы!";
        } catch (NoSquareException ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Ошибка при формированиии матрицы условий";
        } catch (InitialPlanCannotbeFinded ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Начальный план не может быть найден";
        } catch(TaskHasNotSpecialForm ex){
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Задача имеем не подходящее для данного метода решение";
        }
        catch (Exception ex) {
            Logger.getLogger(IndexFrame.class.getName()).log(Level.SEVERE, null, ex);
            errorMessage = "Неизвестная ошибка при вычислении";
        }
        if(!errorMessage.isEmpty())
            JOptionPane.showMessageDialog(null, errorMessage);
        if (isOptimalPlanFinded) {
            StringBuilder builder = new StringBuilder();
            builder.append("[");
            for (int i : solver.getBasisVectorsId()) {
                builder.append("A" + (i + 1) + " ");
            }
            builder.append("]");
            lOptimalBasis.setText(builder.toString());
            double [] optimalplan = solver.getOptimalPlan();
            for(int i = 0; i < optimalplan.length; i++)
                optimalplan[i] = Matrix.round(optimalplan[i], 3);
            lOptimalPlanDirectTask.setText(Arrays.toString(optimalplan));
            lOptimalValueDirectFunction.setText(solver.getFunctionOptimalValue().toString());
            lCalculatingTime.setText(Long.toString(endTime - startTime));
            lSimplexTableNumber.setText(Integer.toString(solver.getSimplexTables().size()));
            lAccebilityResult.setText(admissibilityCheckingResult ? "Положительный" : "Отрицательный");
        } else {
            JOptionPane.showMessageDialog(this, errorMessage, "Ошибка", JOptionPane.ERROR);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable BusinessRestrictionsTable;
    private javax.swing.JTable BusinessRestrictionsTable1;
    private javax.swing.JButton bAddBusinessRestriction;
    private javax.swing.JButton bAddColumn;
    private javax.swing.JButton bAdmissibilityCheck;
    private javax.swing.JButton bCheckingFoothold;
    private javax.swing.JButton bGenerateReport;
    private javax.swing.JButton bMusculoskeletalCheck;
    private javax.swing.JButton bOptimalityCheck;
    private javax.swing.JButton bRemoveBusinessRestriction;
    private javax.swing.JButton bRemoveColumn;
    private javax.swing.JButton bSolveTask;
    private javax.swing.JTable directFunctionTable;
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lAccebilityResult;
    private javax.swing.JLabel lCalculatingTime;
    private javax.swing.JLabel lOptimalBasis;
    private javax.swing.JLabel lOptimalPlanDirectTask;
    private javax.swing.JLabel lOptimalValueDirectFunction;
    private javax.swing.JLabel lResultCheckForMusculoskeletal;
    private javax.swing.JLabel lResultCheckForOptimality;
    private javax.swing.JLabel lSimplexTableNumber;
    private javax.swing.JMenuItem miCreateNewTask;
    private javax.swing.JMenuItem miLoadTask;
    private javax.swing.JMenuItem miSaveTask;
    private javax.swing.JMenuItem miSolveTask;
    // End of variables declaration//GEN-END:variables
    private javax.swing.JComboBox directFunctionTableExtremum;
    private javax.swing.JComboBox businessRestrictionsTableInequantity;
    private DirectFunctionTableModel directFunctionTableModel;
    private BusinessRestrictionsTableModel businessRestrictionsTableModel;
}
